//
//  SensorsController.m
//  Pennyworth
//
//  Created by Chris Karr on 8/15/08.
//  Copyright 2008 Northwestern University. All rights reserved.
//

#import "SensorsController.h"
#import "AppDelegate.h"

#define SENSOR_PREFS @"Enabled_Sensors"

@implementation SensorsController

static SensorsController * sharedController;

- (SensorsController *) init
{
	if (self = [super init])
		sharedController = self;
	
	return self;
}
	
+ (SensorsController *) sharedController
{
	if (!sharedController)
		sharedController = [[SensorsController alloc] init];
	
	return sharedController;
}

- (BOOL) canFire:(NSString *) sensorName
{
	for (NSDictionary * sensor in [sensors arrangedObjects])
	{
		if ([[sensor valueForKey:@"name"] isEqual:sensorName])
			return [[sensor valueForKey:@"enabled"] boolValue];
	}
	
	return YES;
}

- (void) awakeFromNib
{
	BOOL hasSensors = NO;
	
	for (NSDictionary * sensor in [sensors arrangedObjects])
		hasSensors = hasSensors || [[sensor valueForKey:@"enabled"] boolValue];

	if (!hasSensors)
	{
		NSRunAlertPanel (@"No Sensors Enabled", @"No sensors have been enabled. Please enable some sensors in the preferences.", @"OK", nil, nil);
		
		[((AppDelegate *) [NSApp delegate]) preferences:self];
	}
}
	
- (void) addSensor:(NSString *) sensorName
{
	BOOL found = NO;
	
	for (NSDictionary * sensor in [sensors arrangedObjects])
	{
		if ([[sensor valueForKey:@"name"] isEqual:sensorName])
			found = YES;
	}

	if (!found)
	{
		NSLog (@"add sensor: %@", sensorName);
		
		NSMutableDictionary * sensor = [NSMutableDictionary dictionary];
		[sensor setObject:sensorName forKey:@"name"];
		[sensor setObject:[NSNumber numberWithBool:NO] forKey:@"enabled"];
		
		[sensors addObject:sensor];
	}
}

@end
